﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace VNPT.DoiSoat
{
    public partial class fNewFolder : DevExpress.XtraEditors.XtraForm
    {
        public fNewFolder()
        {
            InitializeComponent();
            this.Shown += (a, e) =>
            {
                if (_conn==null)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Mất kết nối, xin vui lòng thử lại!", "Xác nhận?", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                checkedComboBoxEdit1.Properties.Items.Clear();
                for (int ct = 1; ct <= 12; ct++)
                {
                    checkedComboBoxEdit1.Properties.Items.Add(ct, string.Format("Tháng_{0:D2}", ct),
                        CheckState.Checked, true);
                }
                myTextEdit2.SelectAll();
            };
        }

        System.Net.FtpClient.FtpClient _conn;
        public fNewFolder(System.Net.FtpClient.FtpClient conn):this()
        {
            _conn = conn;
        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            using (var t = new DevExpress.Utils.WaitDialogForm("Vui lòng chờ..", "Đang tạo thư mục..."))
            {
                if (!(bool)radioGroup1.EditValue)
                {
                    var tex = myTextEdit2.Text;
                    if (!string.IsNullOrWhiteSpace(tex))
                    {
                        _conn.CreateDirectory(_conn.GetWorkingDirectory() + "/" + tex);
                        this.Close();
                    }
                }
                else
                {
                    foreach (DevExpress.XtraEditors.Controls.CheckedListBoxItem item in checkedComboBoxEdit1.Properties.GetItems())
                    {
                        if (item.CheckState == CheckState.Checked)
                        {
                            var dir=_conn.GetWorkingDirectory() + "/" + string.Format(myTextEdit1.Text, item.Value);
                            t.SetCaption(string.Format("Đang tạo thư mục {0}...", string.Format(myTextEdit1.Text, item.Value)));
                            _conn.CreateDirectory(dir);
                        }
                    }
                }
            }
            this.Close();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!(bool)radioGroup1.EditValue)
            {
                myTextEdit1.Enabled = !true;
                checkedComboBoxEdit1.Enabled = !true;
                myTextEdit2.Enabled = !false;
                myTextEdit2.SelectAll();
            }
            else
            {
                myTextEdit1.Enabled = true;
                checkedComboBoxEdit1.Enabled = true;
                myTextEdit2.Enabled = false;
            }
        }
    }
}